﻿
namespace Security.Authorizations
{
    using System;
    using System.Web.Security;
    using System.Security.Principal;
    using System.Web.Hosting;
    using System.Web;
    using System.Threading;
    using System.Configuration;
    using System.Collections.Specialized;
    using System.Web.Configuration;
    using System.Configuration.Provider;

    
    public static partial class Authorizations
    {
        //private static bool s_CacheRolesInCookie;
        //private static string s_CookieName;
        //private static string s_CookiePath;
        //private static CookieProtection s_CookieProtection;
        //private static bool s_CookieRequireSSL;
        //private static bool s_CookieSlidingExpiration;
        //private static int s_CookieTimeout;
        //private static bool s_CreatePersistentCookie;
        //private static string s_Domain;

        private static bool s_Enabled;
        private static bool s_EnabledSet;
        private static bool s_Initialized;
        private static Exception s_InitializeException = null;
        private static readonly object s_lock = new object();
        private static int s_MaxCachedResults = 0x19;
        private static AuthorizationProvider s_Provider;
        private static AuthorizationProviderCollection s_Providers;

        public static void AddUsersToRole(string[] usernames, string roleName)
        {
            AddUsersToRole(usernames, roleName, null);
        }
        public static void AddUsersToRole(string[] usernames, string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 0, "usernames");

            Provider.AddUsersToRoles(usernames, new string[] { roleName }, scopeName);
            try
            {
                var currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && currentUser.IsRoleListCached)
                {
                    foreach (string str in usernames)
                    {
                        if (currentUser.Identity.Name.Equals(str, StringComparison.OrdinalIgnoreCase))
                        {
                            currentUser.SetDirty();
                            return;
                        }
                    } 
                }
            }
            catch 
            {
            }
        }

        public static void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            AddUsersToRoles(usernames, roleNames, null);
        }
        public static void AddUsersToRoles(string[] usernames, string[] roleNames, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 0, "roleNames");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 0, "usernames");
            
            Provider.AddUsersToRoles(usernames, roleNames, scopeName);
            try
            {
                RolePrincipal currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && currentUser.IsRoleListCached)
                {
                    foreach (string str in usernames)
                    {
                        if (currentUser.Identity.Name.Equals(str, StringComparison.OrdinalIgnoreCase))
                        {
                            currentUser.SetDirty();
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
        }

        public static void AddUserToRole(string username, string roleName)
        {
            AddUserToRole(username, roleName, null);
        }

        public static void AddUserToRole(string username, string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckParameter(ref username, true, true, true, 0, "username");
            Provider.AddUsersToRoles(new string[] { username }, new string[] { roleName }, scopeName);

            try
            {
                RolePrincipal currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && (currentUser.IsRoleListCached && currentUser.Identity.Name.Equals(username, StringComparison.OrdinalIgnoreCase)))
                {
                    currentUser.SetDirty();
                }
            }
            catch
            {
            }
        }

        public static void AddUserToRoles(string username, string[] roleNames)
        {
            AddUserToRoles(username, roleNames, null);
        }

        public static void AddUserToRoles(string username, string[] roleNames, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref username, true, true, true, 0, "username");
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 0, "roleNames");
            Provider.AddUsersToRoles(new string[] { username }, roleNames, scopeName);

            try
            {
                var currentUser = GetCurrentUser() as RolePrincipal;
                //if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && (currentUser.IsRoleListCached && StringUtil.EqualsIgnoreCase(currentUser.Identity.Name, username)))
                //{
                //    currentUser.SetDirty();
                //}
            }
            catch
            {
            }
        }


        public static void CreateRole(string roleName, string roleAssignmentName, string description, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleAssignmentName");

            Provider.CreateRole(roleName, roleAssignmentName, description, scopeName);
        }

        public static void CreateRole(string roleName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            Provider.CreateRole(roleName);
        }

        //public static void DeleteCookie()
        //{
        //    EnsureEnabled();
        //    if ((CookieName != null) && (CookieName.Length >= 1))
        //    {
        //        HttpContext current = HttpContext.Current;
        //        if ((current != null) && current.Request.Browser.Cookies)
        //        {
        //            string str = string.Empty;
        //            if (current.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
        //            {
        //                str = "NoCookie";
        //            }
        //            HttpCookie cookie = new HttpCookie(CookieName, str);
        //            cookie.HttpOnly = true;
        //            cookie.Path = CookiePath;
        //            cookie.Domain = Domain;
        //            cookie.Expires = new DateTime(0x7cf, 10, 12);
        //            cookie.Secure = CookieRequireSSL;
        //            current.Response.Cookies.Remove(CookieName);
        //            current.Response.Cookies.Add(cookie);
        //        }
        //    }
        //}

        public static bool DeleteRole(string roleName, string scopeName)
        {
            return DeleteRole(roleName, true, scopeName);
        }

        public static bool DeleteRole(string roleName)
        {
            return DeleteRole(roleName, true, null);
        }

        public static bool DeleteRole(string roleName, bool throwOnPopulatedRole, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
          
            bool flag = Provider.DeleteRole(roleName, throwOnPopulatedRole, scopeName);

            try
            {
                RolePrincipal currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && (currentUser.IsRoleListCached && currentUser.IsInRole(roleName)))
                {
                    currentUser.SetDirty();
                }
            }
            catch
            {
            }
            return flag;
        }

        private static void EnsureEnabled()
        {
            Initialize();
            if (!s_Enabled)
            {
                throw new ProviderException("Authorizations_feature_not_enabled");
            }
        }

        public static string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 0, "usernameToMatch");
            return Provider.FindUsersInRole(roleName, usernameToMatch);
        }

        public static string[] FindUsersInScope(string scopeName, string usernameToMatch)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref scopeName, true, true, true, 0, "scopeName");
            SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 0, "usernameToMatch");
            return Provider.FindUsersInScope(scopeName, usernameToMatch);
        }


        public static string[] GetAllOperations()
        {
            EnsureEnabled();
            return Provider.GetAllOperations();
        }

        public static string[] GetAllTasks(string scopeName)
        {
            EnsureEnabled();
            return Provider.GetAllTasks(scopeName);
        }

        public static string[] GetAllTasks()
        {
            EnsureEnabled();
            return Provider.GetAllTasks();
        }

        public static string[] GetAllRoles()
        {
            EnsureEnabled();
            return Provider.GetAllRoles();
        }

        public static string[] GetAllRoles(string scopeName)
        {
            EnsureEnabled();
            return Provider.GetAllRoles(scopeName);
        }

        public static string[] GetAllScopes()
        {
            EnsureEnabled();
            return Provider.GetAllScopes();
        }


        public static string[] GetScopesForUserWithFilter(string filterToMatch)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref filterToMatch, true, true, false, 0, "filterToMatch");
            return Provider.GetScopesForUser(GetCurrentUserName(), filterToMatch);
        }

        public static string[] GetScopesForUserWithFilter(string username, string filterToMatch)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref username, true, true, true, 0, "username");
            SecUtility.CheckParameter(ref filterToMatch, true, true, false, 0, "filterToMatch");
            return Provider.GetScopesForUser(username, filterToMatch);
        }

        public static string[] GetScopesForUser()
        {
            EnsureEnabled();
            return  Provider.GetScopesForUser(GetCurrentUserName());
        }


        public static string[] GetScopesForUser(string username)
        {
            EnsureEnabled();
            return Provider.GetScopesForUser(username);
        }

          public static string[] GetScopesForUser(string username, string filterToMatch)
        {
            EnsureEnabled();
            return Provider.GetScopesForUser(username, filterToMatch);
        }


          public static string[] GetUsersInScope(string scopeName)
          {
              EnsureEnabled();
              return Provider.GetUsersInScope(scopeName);
          }

          public static bool IsUserInScope(string username, string scopeName)
          {
              EnsureEnabled();
              return Provider.IsUserInScope(username, scopeName);

          }
                  
          public static bool IsUserInScope(string scopeName)
          {
              EnsureEnabled();
              return Provider.IsUserInScope(GetCurrentUserName(), scopeName);

          }
          public static bool ScopeExists(string scopeName)
          {
              EnsureEnabled();
              SecUtility.CheckParameter(ref scopeName, true, true, true, 0, "scopeName");
              return Provider.ScopeExists(scopeName);
          }

          public static void CreateScope(string scopeName)
          {
              EnsureEnabled();
              SecUtility.CheckParameter(ref scopeName, true, true, true, 0, "scopeName");
              Provider.CreateScope(scopeName);
          }

          public static void DeleteScope(string scopeName)
          {
              EnsureEnabled();
              SecUtility.CheckParameter(ref scopeName, true, true, true, 0, "scopeName");
              Provider.DeleteScope(scopeName);
          }

        private static IPrincipal GetCurrentUser()
        {
            if (HostingEnvironment.IsHosted)
            {
                HttpContext current = HttpContext.Current;
                if (current != null)
                {
                    return current.User;
                }
            }
            return Thread.CurrentPrincipal;
        }

        private static string GetCurrentUserName()
        {
            IPrincipal currentUser = GetCurrentUser();
            if ((currentUser != null) && (currentUser.Identity != null))
            {
                return currentUser.Identity.Name;
            }

            return string.Empty;
        }

        public static string[] GetRolesForUser()
        {
            return GetRolesForUser(GetCurrentUserName(), null);
        }

        public static string[] GetRolesForUser(string username)
        {
            return GetRolesForUser(GetCurrentUserName(), null);
        }
        public static string[] GetRolesForUser(string username, string scopeName)
        {
            EnsureEnabled();

            string[] roles = null;

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");
            if (username.Length < 1)
            {
                return new string[0];
            }
            IPrincipal currentUser = GetCurrentUser();
            if (((currentUser != null) && (currentUser is RolePrincipal)) && ((((RolePrincipal)currentUser).ProviderName == Provider.Name) && string.Equals(username, currentUser.Identity.Name, StringComparison.OrdinalIgnoreCase)))
            {
                roles = ((RolePrincipal)currentUser).GetRoles();
            }
            else
            {
                roles = Provider.GetRolesForUser(username, scopeName);
            }
            return roles;


        }

        //public abstract bool IsUserInRole(string username, string roleName, string scopeName);
        //public abstract Operation[] GetOperationsForUser(string username);
        //public abstract Operation[] GetOperationsForUser(string username, string scopeName);
        //public abstract bool IsUserInTask(string username, string taskName);
        //public abstract string[] GetTaksForUser(string username);
        //public abstract bool IsUserInOperation(string username, string operationName);


        public static string[] GetUsersInRole(string roleName)
        {
            return GetUsersInRole(roleName, null);
        }

        public static string[] GetUsersInRole(string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            return Provider.GetUsersInRole(roleName,scopeName);
        }


        public static string[] GetOperationsForUser()
        {
            return GetOperationsForUser(GetCurrentUserName(), null);
        }

        public static string[] GetOperationsForUser(string username)
        {
            return GetOperationsForUser(username, null);
        }

        public static string[] GetOperationsForUser(string username, string scopeName)
        {
            EnsureEnabled();

            string[] operations = null;

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return new string[0];
            }
            operations = Provider.GetOperationsForUser(username, scopeName);
            return operations;
        }
        
        
        public static string[] GetTasksForUser()
        {
            return GetTasksForUser(GetCurrentUserName(), null);
        }

        public static string[] GetTasksForUser(string username)
        {
            return GetTasksForUser(username, null);
        }

        public static string[] GetTasksForUser(string username, string scopeName)
        {
            EnsureEnabled();

            string[] tasks = null;

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return new string[0];
            }
            tasks = Provider.GetTasksForUser(username, scopeName);
            return tasks;
        }

        public static bool IsUserInTask(string username, string taskName)
        {
            EnsureEnabled();
            return Provider.IsUserInTask(username, taskName);

        }

        public static bool IsUserInTask(string scopeName)
        {
            EnsureEnabled();
            return Provider.IsUserInTask(GetCurrentUserName(), scopeName);

        }

        
        public static bool AccessCheck(string operationName)
        {
            return AccessCheck(GetCurrentUserName(), operationName, null);
        }

        public static bool AccessCheck(string username, string operationName)
        {
            return AccessCheck(username, operationName, null);
        }

        public static bool AccessCheck(string operationName, string[] scopeNames)
        {
            return AccessCheck(GetCurrentUserName(), operationName, scopeNames);
        }


        public static string[] AccessCheck(string[] operationNames, string[] scopeNames)
        {
            return AccessCheck(GetCurrentUserName(), operationNames, scopeNames);
        }

        public static string[] AccessCheck(string[] operationNames, string scopeName)
        {
            return AccessCheck(GetCurrentUserName(), operationNames, scopeName);
        }

        public static string[] AccessCheck(string username, string[] operationNames, string scopeName)
        {

            EnsureEnabled();

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return null;
            }

            return Provider.AccessCheck(username, operationNames, scopeName);
        }


        public static string[] AccessCheck(string username, string[] operationNames, string[] scopeNames)
        {
            EnsureEnabled();

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return null;
            }

            return Provider.AccessCheck(username, operationNames, scopeNames);
        }

        public static string[] AccessCheckAnyScope(string[] operationNames, string filterToMatch)
        {
            return AccessCheckAnyScope(GetCurrentUserName(), operationNames, filterToMatch);
        }

        public static string[] AccessCheckAnyScope(string username, string[] operationNames, string filterToMatch)
        {
            EnsureEnabled();

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return null;
            }

            return Provider.AccessCheckAnyScope(username, operationNames, filterToMatch);
        }


        public static bool AccessCheck(string username, string operationName, string[] scopeNames)
        {
            EnsureEnabled();

            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");

            if (username.Length < 1)
            {
                return false;
            }

            return Provider.AccessCheck(username, operationName, scopeNames);
        }


        #region Private Methods


        private static void Initialize()
        {
            if (s_Initialized)
            {
                if (s_InitializeException != null)
                {
                    throw s_InitializeException;
                }
            }
            else
            {
                lock (s_lock)
                {
                    if (s_Initialized)
                    {
                        if (s_InitializeException != null)
                        {
                            throw s_InitializeException;
                        }
                        return;
                    }
                    try
                    {

                        var authorizationManager = (AuthorizationManagerSection)ConfigurationManager.GetSection("Security.Authorizations");

                        s_Enabled = authorizationManager.Enabled;
                        //s_CookieName = authorizationManager.CookieName;
                        //s_CacheRolesInCookie = authorizationManager.CacheRolesInCookie;
                        //s_CookieTimeout = (int)authorizationManager.CookieTimeout.TotalMinutes;
                        //s_CookiePath = authorizationManager.CookiePath;
                        //s_CookieRequireSSL = authorizationManager.CookieRequireSSL;
                        //s_CookieSlidingExpiration = authorizationManager.CookieSlidingExpiration;
                        //s_CookieProtection = authorizationManager.CookieProtection;
                        //s_Domain = authorizationManager.Domain;
                        //s_CreatePersistentCookie = authorizationManager.CreatePersistentCookie;
                        s_MaxCachedResults = authorizationManager.MaxCachedResults;

                        if (s_Enabled)
                        {
                            if (s_MaxCachedResults < 0)
                            {
                                throw new ProviderException("Value_must_be_non_negative_integer");
                            }
                            s_Providers = new AuthorizationProviderCollection();
                            if (HostingEnvironment.IsHosted)
                            {
                                ProvidersHelper.InstantiateProviders(authorizationManager.Providers, s_Providers, typeof(AuthorizationProvider));
                            }
                            else
                            {
                                foreach (ProviderSettings settings in authorizationManager.Providers)
                                {
                                    Type c = Type.GetType(settings.Type, true, true);
                                    if (!typeof(AuthorizationProvider).IsAssignableFrom(c))
                                    {
                                        throw new ArgumentException("Provider_must_implement_type");
                                    }
                                    AuthorizationProvider provider = (AuthorizationProvider)Activator.CreateInstance(c);
                                    NameValueCollection parameters = settings.Parameters;
                                    NameValueCollection config = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
                                    foreach (string str in parameters)
                                    {
                                        config[str] = parameters[str];
                                    }
                                    provider.Initialize(settings.Name, config);
                                    s_Providers.Add(provider);
                                }
                            }
                            s_Providers.SetReadOnly();
                            if (authorizationManager.DefaultProvider == null)
                            {
                                s_InitializeException = new ProviderException("Def_role_provider_not_specified");
                            }
                            else
                            {
                                try
                                {
                                    s_Provider = s_Providers[authorizationManager.DefaultProvider];
                                }
                                catch
                                {
                                }
                            }
                            if (s_Provider == null)
                            {
                                s_InitializeException = new ConfigurationErrorsException("Def_role_provider_not_found", authorizationManager.ElementInformation.Properties["defaultProvider"].Source, authorizationManager.ElementInformation.Properties["defaultProvider"].LineNumber);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        s_InitializeException = exception;
                    }
                    s_Initialized = true;
                }
                if (s_InitializeException != null)
                {
                    throw s_InitializeException;
                }
            }
        }

        #endregion

        public static bool IsUserInRole(string roleName)
        {
            return IsUserInRole(GetCurrentUserName(), roleName);
        }

        public static bool IsUserInRole(string username, string roleName)
        {
            return IsUserInRole(username, roleName, null);
        }

        public static bool IsUserInRole(string username, string roleName, string scopeName)
        {
            EnsureEnabled();
            bool flag = false;

            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckParameter(ref username, true, false, true, 0, "username");
            if (username.Length < 1)
            {
                return false;
            }
            IPrincipal currentUser = GetCurrentUser();
            if (((currentUser != null) && (currentUser is RolePrincipal)) && ((((RolePrincipal)currentUser).ProviderName == Provider.Name) && string.Equals(username, currentUser.Identity.Name, StringComparison.OrdinalIgnoreCase)))
            {
                flag = currentUser.IsInRole(roleName);
            }
            else
            {
                flag = Provider.IsUserInRole(username, roleName, scopeName);
            }
            bool result = flag;


            return result;
        }

        public static void RemoveUserFromRole(string username, string roleName)
        {
            RemoveUserFromRole(username, roleName, null);
        }
        public static void RemoveUserFromRole(string username, string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckParameter(ref username, true, true, true, 0, "username");
            Provider.RemoveUsersFromRoles(new string[] { username }, new string[] { roleName }, scopeName);
            
            try
            {
                var currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && (currentUser.IsRoleListCached && string.Equals(currentUser.Identity.Name, username, StringComparison.OrdinalIgnoreCase)))
                {
                    currentUser.SetDirty();
                }
            }
            catch
            {
            }
        }


        public static void RemoveUserFromRoles(string username, string[] roleNames)
        {
            RemoveUserFromRoles(username, roleNames, null);
        }
        public static void RemoveUserFromRoles(string username, string[] roleNames, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref username, true, true, true, 0, "username");
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 0, "roleNames");
            Provider.RemoveUsersFromRoles(new string[] { username }, roleNames, scopeName);
            
            try
            {
                var currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && (currentUser.IsRoleListCached && string.Equals(currentUser.Identity.Name, username, StringComparison.OrdinalIgnoreCase)))
                {
                    currentUser.SetDirty();
                }
            }
            catch
            {
            }
        }

        public static void RemoveUsersFromRole(string[] usernames, string roleName)
        {
             RemoveUsersFromRole(usernames, roleName, null);
        }

        public static void RemoveUsersFromRole(string[] usernames, string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 0, "usernames");
            Provider.RemoveUsersFromRoles(usernames, new string[] { roleName }, scopeName);

            try
            {
                RolePrincipal currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && currentUser.IsRoleListCached)
                {
                    foreach (string str in usernames)
                    {
                        if (string.Equals(currentUser.Identity.Name, str, StringComparison.OrdinalIgnoreCase))
                        {
                            currentUser.SetDirty();
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
        }


        public static void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            RemoveUsersFromRoles(usernames, roleNames, null);
        }

        public static void RemoveUsersFromRoles(string[] usernames, string[] roleNames, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 0, "roleNames");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 0, "usernames");
            Provider.RemoveUsersFromRoles(usernames, roleNames, scopeName);

            try
            {
                RolePrincipal currentUser = GetCurrentUser() as RolePrincipal;
                if (((currentUser != null) && (currentUser.ProviderName == Provider.Name)) && currentUser.IsRoleListCached)
                {
                    foreach (string str in usernames)
                    {
                        if (string.Equals(currentUser.Identity.Name, str, StringComparison.OrdinalIgnoreCase))
                        {
                            currentUser.SetDirty();
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
        }

        public static bool RoleExists(string roleName)
        {
            return RoleExists(roleName, null);
        }

        public static bool RoleExists(string roleName, string scopeName)
        {
            EnsureEnabled();
            SecUtility.CheckParameter(ref roleName, true, true, true, 0, "roleName");
            return Provider.RoleExists(roleName, scopeName);
        }

        public static string ApplicationName
        {
            get
            {
                return Provider.ApplicationName;
            }
            set
            {
                Provider.ApplicationName = value;
            }
        }

        //public static bool CacheRolesInCookie
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CacheRolesInCookie;
        //    }
        //}

        //public static string CookieName
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookieName;
        //    }
        //}

        //public static string CookiePath
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookiePath;
        //    }
        //}

        //public static CookieProtection CookieProtectionValue
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookieProtection;
        //    }
        //}

        //public static bool CookieRequireSSL
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookieRequireSSL;
        //    }
        //}

        //public static bool CookieSlidingExpiration
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookieSlidingExpiration;
        //    }
        //}

        //public static int CookieTimeout
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CookieTimeout;
        //    }
        //}

        //public static bool CreatePersistentCookie
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_CreatePersistentCookie;
        //    }
        //}

        //public static string Domain
        //{
        //    get
        //    {
        //        Initialize();
        //        return s_Domain;
        //    }
        //}

        public static bool Enabled
        {
            get
            {
                if (!s_Initialized && !s_EnabledSet)
                {
                    s_Enabled = ((AuthorizationManagerSection)ConfigurationManager.GetSection("SiteProviders.Authorization")).Enabled;
                    s_EnabledSet = true;
                }
                return s_Enabled;
            }
        }

        public static int MaxCachedResults
        {
            get
            {
                Initialize();
                return s_MaxCachedResults;
            }
        }

        public static AuthorizationProvider Provider
        {
            get
            {
                EnsureEnabled();
                return s_Provider;
            }
        }

        public static AuthorizationProviderCollection Providers
        {
            get
            {
                EnsureEnabled();
                return s_Providers;
            }
        }

    }
}
